package com.whfc.common.enums;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.whfc.common.util.MathUtil;

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum WindLevel {

    LEVEL0(0, "无风", 0.0, 0.2),
    LEVEL1(1, "软风", 0.3, 1.5),
    LEVEL2(2, "轻风", 1.6, 3.3),
    LEVEL3(3, "微风", 3.4, 5.4),
    LEVEL4(4, "和风", 5.5, 7.9),
    LEVEL5(5, "清风", 8.0, 10.7),
    LEVEL6(6, "强风", 10.8, 13.8),
    LEVEL7(7, "劲风", 13.9, 17.1),
    LEVEL8(8, "大风", 17.2, 20.7),
    LEVEL9(9, "烈风", 20.8, 24.4),
    LEVEL10(10, "狂风", 24.5, 28.4),
    LEVEL11(11, "暴风", 28.5, 32.6),
    LEVEL12(12, "台风", 32.7, 36.9),
    LEVEL13(13, "台风", 37.0, 41.4),
    LEVEL14(14, "强台风", 41.5, 46.1),
    LEVEL15(15, "强台风", 46.2, 50.9),
    LEVEL16(16, "超强台风", 51.0, 56.0);

    private Integer level;

    private String name;

    private Double minSpeed;

    private Double maxSpeed;

    WindLevel(Integer level, String name, Double minSpeed, Double maxSpeed) {
        this.level = level;
        this.name = name;
        this.minSpeed = minSpeed;
        this.maxSpeed = maxSpeed;
    }

    public Integer getLevel() {
        return level;
    }

    public String getName() {
        return name;
    }

    public Double getMinSpeed() {
        return minSpeed;
    }

    public Double getMaxSpeed() {
        return maxSpeed;
    }

    /**
     * 根据风速获取风力等级
     *
     * @param speed
     * @return
     */
    public static WindLevel parseWindLevelBySpeed(Double speed) {
        if(speed == null){
            return LEVEL0;
        }
        double scaledSpeed = MathUtil.round(speed, 1);
        if (scaledSpeed < 0.0D) {
            return LEVEL0;
        }
        WindLevel[] windLevels = WindLevel.values();
        for (WindLevel windLevel : windLevels) {
            if (scaledSpeed >= windLevel.getMinSpeed()
                    && scaledSpeed <= windLevel.getMaxSpeed()) {
                return windLevel;
            }
        }
        return LEVEL0;
    }
}
